import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.swing.*;

/**
 *
 * @author hazumu
 */
public class NewSOundSetting extends JFrame {
	private Clip currentSoundClip = null;
	String selectedSong = null;
    
    public NewSOundSetting() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {

        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        jLabel3 = new JLabel();
        alarmSoundComboBox = new JComboBox();
        reminderSoundComboBox = new JComboBox();
        playAlarmSoundButton = new JButton();
        playReminderSoundButton = new JButton();
        saveButton = new JButton();
        cancelButton = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("DejaVu Sans", 1, 36)); // NOI18N
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel1.setText("Sounds & Alert");

        jLabel2.setFont(new java.awt.Font("DejaVu Sans", 0, 18)); // NOI18N
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Alarm Clock: ");

        jLabel3.setFont(new java.awt.Font("DejaVu Sans", 0, 18)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Reminder: ");
        
        alarmSoundComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose", "lion roar", "row", "horse exhale" }));
        alarmSoundComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	alarmSoundComboBoxActionPerformed(evt);
            }
        });

        reminderSoundComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Choose", "lion roar", "row", "horse exhale" }));
        reminderSoundComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	reminderSoundComboBoxActionPerformed(evt);
            }
        });

        playAlarmSoundButton.setText("PLAY");
        playAlarmSoundButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	playAlarmSoundButtonActionPerformed(evt);
            }
        });

        playReminderSoundButton.setText("PLAY");
        playReminderSoundButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	playReminderSoundButtonActionPerformed(evt);
            }
        });

        saveButton.setText("SAVE");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("CANCEL");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(alarmSoundComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(reminderSoundComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(playAlarmSoundButton)
                            .addComponent(playReminderSoundButton)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(cancelButton)
                        .addGap(18, 18, 18)
                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 408, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jLabel1)
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(alarmSoundComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(playAlarmSoundButton))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(reminderSoundComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(playReminderSoundButton))
                .addGap(75, 75, 75)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(saveButton))
                .addGap(25, 25, 25))
        );
        pack();
    }

    private void alarmSoundComboBoxActionPerformed(ActionEvent evt) {
    	File file = null;
   	 	selectedSong = (String) alarmSoundComboBox.getSelectedItem();
   	 	
   		if(selectedSong == "lion roar") {
   			String fileName = "lion_roar.wav";	
   			try { 
   				BufferedWriter out = new BufferedWriter(new FileWriter("songName")); 
   				out.write("lion_roar.wav"); out.close(); } 
   			catch (IOException e) { }
   		}
   		else if(selectedSong == "row") {
   			String fileName = "row.wav";
   			file = new File(fileName);
   			try { 
   				BufferedWriter out = new BufferedWriter(new FileWriter("songName")); 
   				out.write("row.wav");
   				out.close(); } 
   			catch (IOException e) { } 
   		}
   		else if(selectedSong == "horse exhale") {
   			String fileName = "horse_exhale.wav";
   			file = new File(fileName);
   			try{
   				BufferedWriter out = new BufferedWriter(new FileWriter("songName"));
   				out.write("horse_exhale.wav");
   				out.close();
   			}
   			catch(IOException e){}
   		}
    }

    private void reminderSoundComboBoxActionPerformed(ActionEvent evt) {
    	File file = null;
		selectedSong = (String) reminderSoundComboBox.getSelectedItem();
		if(selectedSong == "lion roar") {
			String fileName = "lion_roar.wav";		
			file = new File(fileName);
			try {
				BufferedWriter out = new BufferedWriter(new FileWriter("RemindersongName")); 
				out.write("lion_roar.wav"); out.close(); } 
			catch (IOException e) { }
		}
		else if(selectedSong == "row") {
			String fileName = "row.wav";
			file = new File(fileName);
			try {
				BufferedWriter out = new BufferedWriter(new FileWriter("RemindersongName"));
				out.write("row.wav");
				out.close(); }
			catch (IOException e) { } 
		}
		else if(selectedSong == "horse exhale") {
			String fileName = "horse_exhale.wav";
			file = new File(fileName);
			try {
				BufferedWriter out = new BufferedWriter(new FileWriter("RemindersongName"));
				out.write("horse_exhale.wav");
				out.close();
			}
			catch(IOException e){}
		}
    }

    private void playAlarmSoundButtonActionPerformed(ActionEvent evt) {
    	String str = "";
    	String abc = "";
    	try {
    		BufferedReader in = new BufferedReader(new FileReader("songName"));
    	
    		while ((str = in.readLine()) != null) {
    			abc = str;
    		}//while end
    		in.close();
    	}//try end
    	catch (IOException e){
    		System.out.println("error");
    	}
    	
    	String fileName = abc;
    	File file = new File(fileName);
    	
    	try {
    		AudioInputStream stream = AudioSystem.getAudioInputStream(file);
    		AudioFormat format = stream.getFormat();
    		
    		if ((format.getEncoding() == AudioFormat.Encoding.ULAW) ||
    			(format.getEncoding() == AudioFormat.Encoding.ALAW)) {
    			AudioFormat tmp = new AudioFormat(
    					AudioFormat.Encoding.PCM_SIGNED,
    					format.getSampleRate(),
    					format.getSampleSizeInBits() * 2,
    					format.getChannels(),
    					format.getFrameSize() * 2,
    					format.getFrameRate(),
    					true);
    			stream = AudioSystem.getAudioInputStream(tmp, stream);
    			format = tmp;
    		}
    		
    		DataLine.Info info = new DataLine.Info(Clip.class,
    				stream.getFormat(),
    				((int) stream.getFrameLength() * format.getFrameSize()));
    		
    		currentSoundClip = (Clip) AudioSystem.getLine(info);
    		currentSoundClip.open(stream);
    	} catch (Exception ex) {
    		currentSoundClip = null;
    	}
    	currentSoundClip.start(); 
    }

    private void playReminderSoundButtonActionPerformed(ActionEvent evt) {
    	String str = "";
    	String abc = "";
    	try {
    		BufferedReader in = new BufferedReader(new FileReader("RemindersongName"));
    		
    		while ((str = in.readLine()) != null) {
    			abc = str;
		   	}//while end
    		
    		in.close();
    	}//try end
    	catch (IOException e){
    		System.out.println("error");
    	}
    	
    	String fileName = abc;
    	File file = new File(fileName);
    	
    	try {
    		AudioInputStream stream = AudioSystem.getAudioInputStream(file);
    		AudioFormat format = stream.getFormat();
    		
    		if ((format.getEncoding() == AudioFormat.Encoding.ULAW) ||
    			(format.getEncoding() == AudioFormat.Encoding.ALAW)) {
    			AudioFormat tmp = new AudioFormat(
    					AudioFormat.Encoding.PCM_SIGNED,
    					format.getSampleRate(),
    					format.getSampleSizeInBits() * 2,
    					format.getChannels(),
    					format.getFrameSize() * 2,
    					format.getFrameRate(),
    					true);
    			stream = AudioSystem.getAudioInputStream(tmp, stream);
    			format = tmp;
    		}
    		
    		DataLine.Info info = new DataLine.Info(Clip.class,
    				stream.getFormat(),
    				((int) stream.getFrameLength() * format.getFrameSize()));
    		
    		currentSoundClip = (Clip) AudioSystem.getLine(info);
    		currentSoundClip.open(stream);
    	} catch (Exception ex) {
    		currentSoundClip = null;
    	}
    	
    	currentSoundClip.start(); 
    }

    private void cancelButtonActionPerformed(ActionEvent evt) {
    	  this.setVisible(false);
    }
    
    private void saveButtonActionPerformed(ActionEvent evt) {
    	int save = JOptionPane.showConfirmDialog(this,"Save the changes made? ","Alarm Clock",JOptionPane.YES_NO_OPTION);
        if(save == JOptionPane.YES_NO_OPTION){
        	try {
        		
            	JOptionPane.showMessageDialog(this,"Your information have been saved","Alarm Clock",JOptionPane.INFORMATION_MESSAGE);
            }
            catch(Exception e){
                JOptionPane.showMessageDialog(this,"WARNING : "+ e.getMessage(),"Alarm Clock",JOptionPane.WARNING_MESSAGE);
            }
        }
    }

    
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewSOundSetting().setVisible(true);
            }
        });
    }

    
    private javax.swing.JButton playAlarmSoundButton;
    private javax.swing.JButton playReminderSoundButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JComboBox alarmSoundComboBox;
    private javax.swing.JComboBox reminderSoundComboBox;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
}
